﻿@model Books.Web.ViewModels.PublisherDetailViewModel

@Html.HeadTitle("Publisher Detail")
@Html.jqModel("PublisherDetail", Model)
@Html.Partial("_TopNav")

@Html.Partial("_PublisherDialog")

<div id="tabContent">
    <fieldset>
        <legend>Publisher</legend>

        <div id="publisherInfo" class="info"></div>
        <div class="buttonBar"></div>
    </fieldset>

    <fieldset>
        <legend>Books</legend>

        <table id="booksTable" class="table1">
            <tr>
                <th>Title</th>
                <th>Year</th>
                <th>Authors</th>
            </tr>
        </table>
    </fieldset>

    <fieldset>
        <legend>Authors</legend>

        <table id="authorsTable" class="table1">
            <tr>
                <th>Last Name</th>
                <th>First Name</th>
                <th>Born</th>
                <th>Died</th>
            </tr>
        </table>
    </fieldset>
</div>

<script id="publisherTemplate" type="text/x-jquery-tmpl">
    <label class="fieldLabel">Publisher Name</label>${PublisherName}&nbsp;&nbsp;
    <label class="fieldLabel">City</label>${City}
</script>

<script id="booksTemplate" type="text/x-jquery-tmpl">
    <tr>
        <td>${Title}</td>
        <td>${PublishedYear}</td>
        <td>
            {{each(index, author) Authors}}
                {{if index > 0}}
                    <span style="margin-left:-4px">,&nbsp;</span>
                {{/if}}
                <a href='@Url.Action("AuthorDetail", "Author")/${author.AuthorId}'>${author.FullName}</a>
            {{/each}}
        </td>
    </tr>
</script>

<script id="authorsTemplate" type="text/x-jquery-tmpl">
    <tr>
        <td>${LastName}</td>
        <td>${FirstName}</td>
        <td>${Born}</td>
        <td>${Died}</td>
    </tr>
</script>

<script type="text/javascript">
    function DeletePublisher() {
        $('button').blur();
        if (confirm("Do you really want to delete this publisher?")) {
            $.post('@Url.Action("PublisherDelete")', { id: PublisherDetail.Publisher.PublisherId }, function () {
                window.location = '@Url.Action("Index")';
            });
        }
    }

    function EditPublisher() {
        $('button').blur();
        PublisherDialog.open(PublisherDetail.Publisher, function (wasSaved) {
            if (wasSaved) {
                $('#publisherInfo').empty();
                $('#publisherTemplate').tmpl(PublisherDialog.publisher).appendTo('#publisherInfo');
            }
        });
    }

    $(function () {
        $('#publisherTemplate').tmpl(PublisherDetail.Publisher).appendTo('#publisherInfo');
        $('#booksTemplate').tmpl(PublisherDetail.Books).appendTo('#booksTable');
        $('#authorsTemplate').tmpl(PublisherDetail.Authors).appendTo('#authorsTable');

        $('.buttonBar').buttons([
            { icons: { primary: "ui-icon-pencil" }, label: "Edit", click: EditPublisher },
            { icons: { primary: "ui-icon-trash" }, label: "Delete", click: DeletePublisher }
        ]);
    });
</script>